# -*- coding: utf-8 -*-
import os
import re
import threading
import json
from RankComic import ranking
from NovelDB import updateNovel

from pymongo import MongoClient

import requests

shortBaseUrl = 'http://uiu.pub/'
# shortRequestUrl = 'http://uiu.pub/api'
shortRequestUrl = 'http://localhost:8080/shorten'
# shortRequestUrl = 'http://192.168.0.211:1888/api'
tu = u'图：'
wen = u'文：'


def getShortUrl(originUrl):
    print('---------请求shorterUrl------')
    param = {'originUrl': originUrl}
    response = requests.get(shortRequestUrl, params=param)
    content = json.loads(bytes.decode(response.content), encoding='utf-8')
    response.close()
    if content['errorCode'] == 0:
        return shortBaseUrl + content['data']
    else:
        return None


def get_Spider_DB():
    url = "192.168.1.99"
    # url = "localhost"
    port = 27017
    db_name = 'resultdb'
    client = MongoClient(url, port)
    client[db_name].authenticate('xmtj', '0-=p[]P{}', 'admin')
    db = client[db_name]
    return db


def get_Save_DB():
    url = "192.168.1.99"
    # url = "tit.pub"
    # url = "localhost"
    port = 27017
    db_name = 'comic_spider_db'
    client = MongoClient(url, port)
    client[db_name].authenticate('xmtj', '0-=p[]P{}', 'admin')
    db = client[db_name]
    return db


def filterAndReloadAuthor(originAuthor):
    index = originAuthor.find(tu)
    if index != -1:
        return originAuthor[index:]
    index = originAuthor.find(wen)
    if index != -1:
        return originAuthor[index:]
    return originAuthor


mongoDB = get_Spider_DB()
mongoSaveDB = get_Save_DB()

mongoSaveIpCollection = mongoSaveDB.all_ip_name
mongoSaveAuthorCollection = mongoSaveDB.all_author_name

# qq
mongoQQCollection = mongoDB.base_qq
mongoSaveQQCollection = mongoSaveDB.ac_qq_com


def queryAndSaveQQ():
    print('start qq db update')
    global mongoQQCollection
    global mongoSaveQQCollection
    global mongoSaveIpCollection
    global mongoSaveAuthorCollection
    # 删除旧漫画
    # mongoSaveQQCollection.drop()
    print('qq db count:', mongoQQCollection.count())
    qq = mongoQQCollection.find()
    for one in qq:
        try:
            # jobject = json.loads(one['result'], encoding='utf-8')
            ones = mongoSaveQQCollection.find_one_and_update({'url': one['url'], 'title': one['title']}, {
                '$set': {'url': one['url'], 'description': one['description'], 'read_count': one['read_count'],
                         'title': one['title'], 'author': one['author'], 'last_chapter': one['last_chapter'],
                         'like_count': one['like_count'], 'cover': one['cover'], 'theme': one['theme']}},
                                                             upsert=True)
            temShortUrl = None
            try:
                temShortUrl = ones['short_url']

            except:
                pass
            if temShortUrl == None:
                mongoSaveQQCollection.find_one_and_update(
                    {'url': one['url'], 'title': one['title'], 'short_url': None},
                    {'$set': {'short_url': getShortUrl(one['url'])}})

            if mongoSaveIpCollection.find({'ip_name': re.compile(one['title'], re.IGNORECASE)}).count() == 0:
                mongoSaveIpCollection.insert(
                    {'ip_name': one['title'], 'author': filterAndReloadAuthor(one['author'])})
            else:
                print('ip db contain this ip:', one['title'])
                pass

            if mongoSaveAuthorCollection.find({'author': re.compile(one['author'], re.IGNORECASE)}).count() == 0:
                mongoSaveAuthorCollection.insert({'author': filterAndReloadAuthor(one['author'])})
            else:
                print('author db contain this author:', one['author'])
                pass

        except Exception as e:
            print("qq Exception:", e)
            # mongoQQCollection.remove({'_id': ObjectId(one['_id'])})
            # for aa in mongoQQCollection.find({'_id': ObjectId(one['_id'])}):
    print('qq db update finished')


# 快看
mongoKuaiKanCollection = mongoDB.base_kuaikan
mongoSaveKuaiKanCollection = mongoSaveDB.kuaikan


def queryAndSaveKuaikan():
    print('start kuaikan db update')
    global mongoKuaiKanCollection
    global mongoSaveKuaiKanCollection
    global mongoSaveIpCollection
    global mongoSaveAuthorCollection
    # 删除旧漫画
    # mongoSaveKuaiKanCollection.drop()
    kuaikan = mongoKuaiKanCollection.find()
    print('kuaikan db count:', mongoKuaiKanCollection.count())
    for one in kuaikan:
        try:
            # jobject = json.loads(one['result'], encoding='utf-8')
            ones = mongoSaveKuaiKanCollection.find_one_and_update({'url': one['url'], 'title': one['title']},
                                                                  {'$set': {'url':
                                                                                one['url'],
                                                                            'description': one['description'],
                                                                            'read_count': one['read_count'],
                                                                            'title':
                                                                                one[
                                                                                    'title'],
                                                                            'author':
                                                                                one[
                                                                                    'author'],
                                                                            'last_chapter':
                                                                                one[
                                                                                    'last_chapter'],
                                                                            'like_count':
                                                                                one[
                                                                                    'like_count'],
                                                                            'cover':
                                                                                one[
                                                                                    'cover'],
                                                                            'comment_count':
                                                                                one[

                                                                                    'comment_count']}}, upsert=True)
            temShortUrl = None
            try:
                temShortUrl = ones['short_url']

            except:
                pass
            if temShortUrl == None:
                mongoSaveKuaiKanCollection.find_one_and_update(
                    {'url': one['url'], 'title': one['title'], 'short_url': None},
                    {'$set': {'short_url': getShortUrl(one['url'])}})

            if mongoSaveIpCollection.find({'ip_name': re.compile(one['title'], re.IGNORECASE)}).count() == 0:
                mongoSaveIpCollection.insert(
                    {'ip_name': one['title'], 'author': filterAndReloadAuthor(one['author'])})
            else:
                print('ip db contain this ip:', one['title'])
                pass

            if mongoSaveAuthorCollection.find({'author': re.compile(one['author'], re.IGNORECASE)}).count() == 0:
                mongoSaveAuthorCollection.insert({'author': filterAndReloadAuthor(one['author'])})
            else:
                print('author db contain this author:', one['author'])
                pass
        except Exception as e:
            print("kuaikan Exception:", e)
    print('kuaikan db update finished')


# 163
mongo163Collection = mongoDB.base_163
mongoSave163Collection = mongoSaveDB.manhua_163_com


def queryAndSave163():
    print('start m163 db update')
    global mongo163Collection
    global mongoSave163Collection
    global mongoSaveIpCollection
    global mongoSaveAuthorCollection
    # 删除旧漫画
    # mongoSave163Collection.drop()
    print('m163 db count:', mongo163Collection.count())
    m163 = mongo163Collection.find()
    for one in m163:
        try:
            # jobject = json.loads(one['result'], encoding='utf-8')
            ones = mongoSave163Collection.find_one_and_update({'url': one['url'], 'title': one['title']}, {'$set':
                                                                                                               {'url':
                                                                                                                    one[
                                                                                                                        'url'],
                                                                                                                'description':
                                                                                                                    one[
                                                                                                                        'description'],
                                                                                                                'theme':
                                                                                                                    one[
                                                                                                                        'theme'],
                                                                                                                'title':
                                                                                                                    one[
                                                                                                                        'title'],
                                                                                                                'author':
                                                                                                                    one[
                                                                                                                        'author'],
                                                                                                                'last_chapter':
                                                                                                                    one[
                                                                                                                        'last_chapter'],
                                                                                                                'like_count':
                                                                                                                    one[
                                                                                                                        'like_count'],
                                                                                                                'cover':
                                                                                                                    one[
                                                                                                                        'cover'],
                                                                                                                'status':
                                                                                                                    one[
                                                                                                                        'status']}},
                                                              upsert=True)
            temShortUrl = None
            try:
                temShortUrl = ones['short_url']

            except:
                pass
            if temShortUrl == None:
                mongoSave163Collection.find_one_and_update(
                    {'url': one['url'], 'title': one['title'], 'short_url': None},
                    {'$set': {'short_url': getShortUrl(one['url'])}})

            if mongoSaveIpCollection.find({'ip_name': re.compile(one['title'], re.IGNORECASE)}).count() == 0:
                mongoSaveIpCollection.insert(
                    {'ip_name': one['title'], 'author': filterAndReloadAuthor(one['author'])})
            else:
                print('ip db contain this ip:', one['title'])
                pass

            if mongoSaveAuthorCollection.find({'author': re.compile(one['author'], re.IGNORECASE)}).count() == 0:
                mongoSaveAuthorCollection.insert({'author': filterAndReloadAuthor(one['author'])})
            else:
                print('author db contain this author:', one['author'])
                pass
        except Exception as e:
            print("m163 Exception:", e)
    print('m163 db update finished')


# u17
mongoU17Collection = mongoDB.base_u17
mongoSaveU17Collection = mongoSaveDB.u17


def queryAndSaveU17():
    print('start u17 db update')
    global mongoU17Collection
    global mongoSaveU17Collection
    global mongoSaveIpCollection
    global mongoSaveAuthorCollection
    # 删除旧漫画
    # mongoSaveU17Collection.drop()
    print('u17 db count:', mongoU17Collection.count())
    u17 = mongoU17Collection.find()
    for one in u17:
        try:
            # jobject = json.loads(one['result'], encoding='utf-8')

            ones = mongoSaveU17Collection.find_one_and_update({'url': one['url'], 'title': one['title']}, {
                '$set': {'url': one['url'], 'description': one['description'], 'theme': one['theme'],
                         'title': one['title'], 'author': one['author'],
                         'last_chapter': "第" + one['last_chapter_index'] + "章",
                         'like_count': one['like_count'], 'cover': one['cover']}}, upsert=True)
            temShortUrl = None
            try:
                temShortUrl = ones['short_url']

            except:
                pass
            if temShortUrl == None:
                mongoSaveU17Collection.find_one_and_update(
                    {'url': one['url'], 'title': one['title'], 'short_url': None},
                    {'$set': {'short_url': getShortUrl(one['url'])}})
            if mongoSaveIpCollection.find({'ip_name': re.compile(one['title'], re.IGNORECASE)}).count() == 0:
                mongoSaveIpCollection.insert(
                    {'ip_name': one['title'], 'author': filterAndReloadAuthor(one['author'])})
            else:
                print('ip db contain this ip:', one['title'])
                pass

            if mongoSaveAuthorCollection.find({'author': re.compile(one['author'], re.IGNORECASE)}).count() == 0:
                mongoSaveAuthorCollection.insert({'author': filterAndReloadAuthor(one['author'])})
            else:
                print('author db contain this author:', one['author'])
                pass
        except Exception as e:
            print("u17 Exception:", e)
    print('u17 db update finished')


# zymk
mongoZYMKCollection = mongoDB.base_zymk
mongoSaveZYMKCollection = mongoSaveDB.zymk


def queryAndSaveZYMK():
    print('start zymk db update')
    global mongoZYMKCollection
    global mongoSaveZYMKCollection
    global mongoSaveIpCollection
    global mongoSaveAuthorCollection
    # 删除旧漫画
    # mongoSaveZYMKCollection.drop()
    print('zymk db count:', mongoZYMKCollection.count())
    zymk = mongoZYMKCollection.find()
    for one in zymk:
        try:
            ones = mongoSaveZYMKCollection.find_one_and_update({'url': one['url'], 'title': one['title']},
                                                               {'$set': {'url': one['url'],
                                                                         'description': one['description'],
                                                                         'title': one['title'], 'author': one['author'],
                                                                         'last_chapter': one['last_chapter_name']}},
                                                               upsert=True)
            temShortUrl = None
            try:
                temShortUrl = ones['short_url']

            except:
                pass
            if temShortUrl == None:
                mongoSaveZYMKCollection.find_one_and_update(
                    {'url': one['url'], 'title': one['title'], 'short_url': None},
                    {'$set': {'short_url': getShortUrl(one['url'])}})

            if mongoSaveIpCollection.find({'ip_name': re.compile(one['title'], re.IGNORECASE)}).count() == 0:
                mongoSaveIpCollection.insert({'ip_name': one['title'], 'author': filterAndReloadAuthor(one['author'])})
            else:
                print('ip db contain this ip:', one['title'])
                pass

            if mongoSaveAuthorCollection.find({'author': re.compile(one['author'], re.IGNORECASE)}).count() == 0:
                mongoSaveAuthorCollection.insert({'author': filterAndReloadAuthor(one['author'])})
            else:
                print('author db contain this author:', one['author'])
                pass
        except Exception as e:
            print("zymk Exception:", e)
    print('zymk db update finished')


def updateDB():
    print("start")
    # 首先同步爬虫数据库数据
    # os.system('./spider299.sh')

    # global mongoSaveIpCollection
    # global mongoSaveAuthorCollection
    # global mongoSaveZYMKCollection
    # # 删除旧漫画
    # mongoSaveIpCollection.drop()
    # mongoSaveAuthorCollection.drop()

    # 更新漫画数据数据库
    # queryAndSave163()
    # queryAndSaveU17()
    # queryAndSaveZYMK()
    # queryAndSaveQQ()
    # queryAndSaveKuaikan()

    # 更新排名数据库
    ranking()
    # 更新小说数据库
    # updateNovel()

    # print(filterAndReloadAuthor('益阅时代 图：益阅时代 文：益阅时代'))
    # print(filterAndReloadAuthor('易书科技 文：《彩色童话全集》编写组'))
    # print(filterAndReloadAuthor('任翔'))


    # global timer
    # timer = threading.Timer(1 * 24 * 60 * 60, updateDB)
    # timer.start()
    # 最后同步99上的数据到tit上
    # os.system('./992TIT_backup.sh')


if __name__ == "__main__":
    timer = threading.Timer(1, updateDB)
    timer.start()
    # print(getShortUrl('http://www.baidu.com'))
